//
//  CZTaskDetailsViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/10.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZTaskDetailsViewController.h"

@interface CZTaskDetailsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *coinLabel;
@property (weak, nonatomic) IBOutlet UILabel *expLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UILabel *medalLabel;
@property (weak, nonatomic) IBOutlet UIButton *acceptBtn;

@end

@implementation CZTaskDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"任务详情";
    [self getTaskData];
    // Do any additional setup after loading the view from its nib.
}

- (void)getTaskData {
    [SVProgressHUD show];
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/task/detail",APP_HOST];
    if (![[NSUserDefaults standardUserDefaults] objectForKey:User_Token]) {
        [SVProgressHUD dismiss];
        CZBaseTabBarController *rootVC = (CZBaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        CZNavigationController *naviVC = rootVC.childViewControllers[0];
        CZMessionCenterViewController *homeVC = (CZMessionCenterViewController *)naviVC.childViewControllers[0];
        CZNavigationController *mineNC = rootVC.childViewControllers[4];
        CZMineViewController *mineVC = mineNC.childViewControllers[0];
        CZUserLoginViewController * loginVC = [[[CZNsjHelper alloc] init] presentLoginViewControllerWithViewCotroller:self];
        @weakify(mineVC);
        loginVC.callBack = ^(){
            [weak_mineVC getUserData];
            [homeVC setUserIconImage];
            weak_mineVC.userIconImageView.yy_imageURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:User_Logo]];
        };
        
        return;
    }
    [GCZAFNetWorking POST:urlString params:@{@"task_id" : self.task_id, @"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token]} success:^(NSDictionary* response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"] isEqualToString:@"200"]) {
            NSDictionary *dataDic = response[@"result"];
            self.titleLabel.text = dataDic[@"task_name"];
            NSString *startTime = dataDic[@"start_date"];
            NSString *endTime = dataDic[@"end_date"];
            NSString *dateTime = [[[startTime substringToIndex:10] stringByAppendingString:@"-"] stringByAppendingString:[endTime substringToIndex:10]];
            self.timeLabel.text = dateTime;
            self.coinLabel.text = dataDic[@"currency_prize"];
            self.expLabel.text = dataDic[@"experience_prize"];
            self.contentLabel.text = dataDic[@"content"];
//            [self setTaskStarWithNumber:task_star.integerValue];
            UIImageView *imgView = [[UIImageView alloc] init];
            imgView.layer.cornerRadius = 10;
            imgView.clipsToBounds = YES;
            [self.containerView addSubview:imgView];
            [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.equalTo(@20);
                make.centerY.equalTo(self.medalLabel);
                make.right.equalTo(self.coinLabel);
            }];
            NSURL *honorUrl = [NSURL URLWithString:dataDic[@"honor_pic"]];
            imgView.yy_imageURL = honorUrl;
            if ([(NSNumber *)dataDic[@"my_status"] isEqual:@1]) {
                self.acceptBtn.backgroundColor = GLOBAL_TEXT_COLOR;
                self.acceptBtn.userInteractionEnabled = NO;
                [self.acceptBtn setTitle:@"已领取" forState:(UIControlStateNormal)];
            }
        } else {
        
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

//- (void)setTaskStarWithNumber:(NSInteger)number {
//    UIImageView *lastView;
//    for (int i = 0; i < number; i ++) {
//        UIImageView *imgView = [[UIImageView alloc] init];
//        [self.containerView addSubview:imgView];
//        imgView.contentMode = UIViewContentModeScaleAspectFit;
////        imgView.backgroundColor = [UIColor redColor];
//        imgView.image = [UIImage imageNamed:@"icon_medal_experience"];
//        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.height.equalTo(@20);
//            make.centerY.equalTo(self.medalLabel);
//            make.right.equalTo(lastView ? lastView.mas_left : self.coinLabel).offset(lastView ? -5 : 0);
//        }];
//        lastView = imgView;
//    }
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (IBAction)acceptTask:(id)sender {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/task/receive",APP_HOST];
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"task_id" : self.task_id} success:^(id response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:@"领取成功"];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
